package IL;

import java.util.Iterator;
import java.util.List;

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

public class ILUtility {

	// Convert java types to C# types
	public static String findType(int jType)
	{
		StringBuffer cType= new StringBuffer("");
		String resolvedType;
		
		switch(jType) {
		
			case Opcodes.T_BOOLEAN:	cType.append("bool");		break;
			case Opcodes.T_CHAR:	cType.append("wchar");		break;
			case Opcodes.T_FLOAT:	cType.append("float32");	break;
			case Opcodes.T_DOUBLE:	cType.append("float62");	break;
			case Opcodes.T_BYTE:	cType.append("int8");		break;
			case Opcodes.T_SHORT:	cType.append("int16");		break;
			case Opcodes.T_INT:		cType.append("int32");		break;	
			case Opcodes.T_LONG:	cType.append("int64");		break;	
			default:											
				break;
		}
		
		resolvedType = new String(cType.toString());
		
		return resolvedType;
	}

	
	// Resolve shortened Type
	public static String resolveShortType(String javaShortType)
	{
		String ilType="";
		StringBuffer sb = new StringBuffer(javaShortType);
		
		if(javaShortType.contains("L") && javaShortType.contains(";"))
		{	
			if(javaShortType.contains("String"))
				ilType = "string";
			else
			{
				sb = sb.replace(0, 1, "class ");
				sb = sb.deleteCharAt(sb.length()-1);
				ilType = sb.toString();
			}
		}
		else{
			
			if(javaShortType.equals("V"))
				ilType = "void";
			else if(javaShortType.equals("Z"))
				ilType = "bool";
			else if(javaShortType.equals("C"))
				ilType = "wchar";
			else if(javaShortType.equals("B"))
				ilType = "int16";
			else if(javaShortType.equals("F"))
				ilType = "float32";
			else if(javaShortType.equals("I"))
				ilType = "int32";
			else if(javaShortType.equals("S"))
				ilType = "int16";
			else if(javaShortType.equals("J"))
				ilType = "int64";
			else if(javaShortType.equals("D"))
				ilType = "float64";
		}	
		
		return ilType;
	}

	
	public static String resolveBranchTarget(String brStatement, List<String> ls)
	{
		int current = -1;
	
		String temp[];
		temp = brStatement.split(" ");
		String label = temp[1];
		String instruction = temp[2];
		
		current = getLabelOffset(label, ls);
		String modifiedIns = new String(instruction+" IL_"+current+"\n");
		
		return modifiedIns;
	}

	
	
	public static int getLabelOffset(String currentLabel, List<String> ls)
	{
		int position = 1;
		int offset = 0;
		
		Iterator<String> it = ls.iterator();

        while(it.hasNext())
        {
        	String value=(String)it.next();

        	if(value.equals(currentLabel)) {
        		offset = position;
        		break;
        	} 
        	position++;   
        }
	
        
		return offset;
	}
	
	
	
	// Get the method parameters in a formatted string from the method description
	
	public static String getFormattedIlParams(Type[] paramList)
	{
		int i;
		StringBuffer temp = new StringBuffer();

		temp.append("(");

		for(i=0; i<paramList.length; i++) {
			if(i>0)
				temp.append(",");
			
			temp.append(resolveShortType(paramList[i].toString()));
		}

		temp.append(")");

		return (temp.toString());
	}


}
